<?php
namespace Home\Controller;
use Think\Controller;
class RegController extends CommonController {
    public function index(){
        // echo "这是注册的控制器";
        // 
        $question = D('verify')->Oneverify();

        $this->assign('title','注册');

      	$this->assign('question',$question);
        // var_dump($question);
        $this->display();
    }
    public function checkData()
    {
    	$password = I('post.password');
    	$repassword = I('post.repassword');

    	if ($password!=$repassword) {
    		$arr = array(
    			'error' => 1,
    			'info' => '两次密码不一致'
    			);
    		echo json_encode($arr);
    		exit;
    	}
    	$verify = I('post.verify');
        // closelog($verify);
        // exit;
        if ($verify!=$_SESSION['answer']) {
            $arr = array(
                'error' => 1,
                'info' => '人类验证不通过'
                );
            echo json_encode($arr);
            exit;
        }
        if (D('user')->checkEamil(I('post.email'))) {
              $arr = array(
                'error' => 1,
                'info' => '邮箱已经存在'
                );
            echo json_encode($arr);
            exit;
        }
        if (D('user')->checkNickName(I('post.nickname'))) {
              $arr = array(
                'error' => 1,
                'info' => '昵称已经存在'
                );
            echo json_encode($arr);
            exit;
        }    
        $data = I('post.');

        $data['password'] = md5($password);

        $data['create_time'] = time();

        $ip = $this->ip();
        $data['city'] = $this->getcity($ip);

        
        $res = M('user')->add($data);
        if ($res) {
             $arr = array(
                'error' => 0,
                'info' => '注册成功'
                );
            echo json_encode($arr);
        }else{
             $arr = array(
                'error' => 1,
                'info' => '注册失败'
                );
            echo json_encode($arr);
        }
    }
    public function ip()
    {
        $ip = get_client_ip();
        return $ip;
    }
    public function getcity($ip)
    {
        $url = "http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;
        
        $res = file_get_contents($url);
        $arr = json_decode($res,true);

        if ($arr['code']==0) {
            return $arr['data']['city'];
        }

    }
}